#CMake版本，务必放在首行
cmake_minimum_required(VERSION 3.0)

include_directories(include)
include_directories(freertos)
include_directories(freertos/port)
include_directories(freertos/include)
set(LIB_PATH ${CMAKE_CURRENT_SOURCE_DIR}/lib) 


#以下用于屏蔽 error: unrecognized command line option ‘-rdynamic’
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

set(CMAKE_C_FLAGS ${C_FLAGS})
set(CMAKE_ASM_FLAGS ${C_FLAGS})
set(CMAKE_EXE_LINKER_FLAGS ${LINK_FLAGS})

add_subdirectory(freertos)
add_subdirectory(uart)
add_subdirectory(asm)

link_directories(${LIB_PATH})
link_libraries(uart asm freertos)

set(EXECUTABLE_OUTPUT_PATH ${BIN_PATH})
add_executable(rv.elf main.c main_blinky.c)

#target_link_options(rv.elf PRIVATE  ${LINK_FLAGS})
#设置变量
set(ELF_FILE ${BIN_PATH}/${PROJECT_NAME}.elf)
set(HEX_FILE ${BIN_PATH}/${PROJECT_NAME}.hex)
set(BIN_FILE ${BIN_PATH}/${PROJECT_NAME}.bin)
set(LST_FILE ${BIN_PATH}/${PROJECT_NAME}.dump)


#PRE_BUILD  - 命令将会在其他依赖项执行前执行
#PRE_LINK   - 命令将会在其他依赖项执行完后执行
#POST_BUILD - 命令将会在目标构建完后执行。
add_custom_command(TARGET "rv.elf" POST_BUILD
    COMMAND ${CMAKE_OBJCOPY} -Obinary ${BIN_PATH}/rv.elf ${BIN_FILE}
    COMMAND ${CMAKE_OBJCOPY} -Oihex  ${BIN_PATH}/rv.elf ${HEX_FILE}
    COMMAND ${CMAKE_OBJDUMP} -S ${BIN_PATH}/rv.elf > ${LST_FILE}

    #打印大小，--format=berkeley 表示使用 Berkeley size 风格，控制输出格式。
    COMMAND ${CMAKE_SIZE} --format=berkeley ${BIN_PATH}/rv.elf ${HEX_FILE}
)